define(['text!html/authentication.html', 'css!styles/authentication.css', 'js/profilePresenter', 'js/adminPresenter', 'js/airlinePresenter', 'js/navigationManager', 'js/serverWrapper', 'knockoutjs'], function (template, _style, profilePresenter, adminPresenter, airlinePresenter, navigationManager, serverWrapper, ko) {
    return new function () {
        var presenter = this;

        presenter.userLoaded = $.Callbacks();
        presenter.user = null;
        presenter.loggedInUserType = null;

        presenter.getUserID = function () {
            return presenter.user.user_id;
        };

        presenter.logoff = function () {
            presenter.user = null;
            presenter.loggedInUserType = null;

            var deleteCookie = function (name) {
                document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
            };

            deleteCookie('UserID');
            deleteCookie('UserType');
            location.reload();
        };

        serverWrapper
            .getLoggedInUser()
            .success(function (user) {
                if ($.isEmptyObject(user)) return;

                var userType = null;
                if (user.Admin_id) userType = 'admin';
                if (user.airline_id) userType = 'airline';
                if (user.user_id) userType = 'member';

                presenter.loggedInUserType = userType;
                presenter.user = user;
                presenter.userLoaded.fire(user.first_name || user.name);
            });

        function viewModel(callback) {
            var self = this;

            self.failed = ko.observable(false);
            self.email = ko.observable('yizhar.gilboa@gmail.com');
            self.password = ko.observable('1234');

            var validate = function () {
                if (self.email() == null ||
                    self.email().length == 0 ||
                    self.password() == null ||
                    self.password().length == 0) {
                    self.onFailure();
                    return false;
                }

                return true;
            };

            self.userLogin = function () {
                if (!validate()) return;

                serverWrapper
                    .authenticate(self.email(), self.password())
                    .success(self.onSuccess)
                    .error(self.onFailure);
            };

            self.airlineLogin = function () {
                if (!validate()) return;

                serverWrapper
                   .authenticateAirline(self.email(), self.password())
                   .success(self.onAirlineSuccess)
                   .error(self.onFailure);
            };

            self.adminLogin = function () {
                if (!validate()) return;

                serverWrapper
                   .authenticateAdmin(self.email(), self.password())
                   .success(self.onAdminSuccess)
                   .error(self.onFailure);
            };

            self.onAirlineSuccess = function (result) {
                if (result && result.status == 1) {
                    presenter.loggedInUserType = 'airline';
                    presenter.user = result.data;
                    airlinePresenter.bind();
                }
                else {
                    self.onFailure();
                }
            };

            self.onAdminSuccess = function (result) {
                if (result && result.status == 1) {
                    presenter.loggedInUserType = 'admin';
                    presenter.user = result.data;
                    adminPresenter.bind();
                }
                else {
                    self.onFailure();
                }
            };

            self.onSuccess = function (result) {
                if (result && result.status == 1) {
                    presenter.loggedInUserType = 'member';
                    presenter.user = result.data;
                    if (callback) {
                        callback();
                    }
                    else {
                        profilePresenter.bind();
                    }
                }
                else {
                    self.onFailure();
                }
            };

            self.onFailure = function () {
                self.failed(true);
            };
        }

        presenter.authenticate = function (callback) {
            if (presenter.loggedInUserType == 'admin') {
                adminPresenter.bind();
            }
            else if (presenter.loggedInUserType == 'airline') {
                airlinePresenter.bind();
            }
            else if (presenter.loggedInUserType == 'member') {
                profilePresenter.bind();
            }
            else {
                navigationManager.load('authentication', template, new viewModel(callback));
            }
        };
    };
});